<template>
	<view class="login-container" v-if="show">
		<view class="wechatapp">
			<view class="header">
				<!-- #ifdef MP-WEIXIN -->
				<open-data class="" type="userAvatarUrl"></open-data>
				<!-- #endif -->
			</view>
		</view>
		<view class="auth-title">申请获取以下权限</view>
		<view class="auth-subtitle">获得你的公开信息（昵称、头像等）</view>
		<view class="login-btn">
			<!-- #ifdef MP-WEIXIN -->
			<!-- <button class="btn-normal" @click="getUserInfo">授权登录</button> -->
			<button class="ss-reset-button type-btn bind-btn" open-type="getPhoneNumber"
				@getphonenumber="getPhoneNumber">
				授权登录
			</button>
			<!-- #endif -->
		</view>
		<view class="no-login-btn">
			<button class="btn-normal" @click="onNotLogin">暂不登录</button>
		</view>
	</view>
	<view v-else>
		<RegitstVue></RegitstVue>
	</view>
</template>

<script>
	import RegitstVue from './regitst.vue';
	export default {
		components: {
			RegitstVue
		},
		data() {
			return {
				background: '',
				listData: [],
				invitationId: 0,
				show: true
			}
		},
		onLoad(e) {
			this.invitationId = uni.getStorageSync('invitationId') ? uni.getStorageSync('invitationId') : 0;
		},
		onShow() {
			//隐藏无效的返回按钮
			if (wx && wx.hideHomeButton) {
				wx.hideHomeButton({
					complete: () => {}
				})
			}

		},
		methods: {
			onNotLogin: function() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			getPhoneNumber(e) {
				let self = this;
				// 检查用户是否授权成功
				if (e.detail.errMsg === 'getPhoneNumber:ok') {
					console.log(e, 'eee')
					// 获取加密数据和加密向量
					const encryptedData = e.detail.encryptedData;
					const iv = e.detail.iv;
					// 执行微信登录
					uni.login({
						success(res_login) {
							console.log(res_login, '执行登录返回的信息')
							// 发送用户信息 user/userWx/login 
							self._post('user/userOpen/login/mobile', {
								invitationId: self.invitationId,
								code: res_login.code,
								// userInfo: res.rawData,
								// encryptedData: encodeURIComponent(res.encryptedData),
								encryptedData: encryptedData,
								iv: iv,
								// signature: res.signature,
								// refereeId: uni.getStorageSync('refereeId')
							}, result => {
								console.log(result, 'result')
								// 记录token userId
								uni.setStorageSync('token', result.data.token);
								uni.setStorageSync('UserState', result.data.status);
								// uni.setStorageSync('userId', result.data.loginUserVo.userId);
								// self.cacheHelper.setUserInfo(res);
								// 执行回调函数,没有上一页,不走回调直接跳转
								// uni.navigateBack();
								if (result.data.registerFlag) {
									uni.switchTab({
										url: '/pages/index/index',
									})
								} else {
									// this.show = false;
									uni.navigateTo({
										url: '/pages/login/regitst'
									})
								}
								//  uni.switchTab({
								// url: '/pages/user/index/index'
								//  });
							}, false, () => {
								uni.hideLoading();
							});
						}
					});
				} else {
					// 用户拒绝授权
					console.log('用户拒绝授权获取手机号');
					uni.showToast({
						title: '你拒绝了授权，无法获取手机号',
						icon: 'none'
					});
				}
			},
			getUserInfo: function() {
				let self = this;
				wx.getUserProfile({
					lang: 'zh_CN',
					desc: '用于完善用户资料',
					success: (res) => {
						console.log(res, '微信返回的用户信息')
						if (res.errMsg !== 'getUserProfile:ok') {
							return false;
						}
						uni.showLoading({
							title: "正在登录",
							mask: true
						});
						// 执行微信登录
						uni.login({
							success(res_login) {
								console.log(res_login, '执行登录返回的信息')
								// 发送用户信息 user/userWx/login 
								self._post('user/userOpen/login/mobile', {
									invitationId: self.invitationId,
									code: res_login.code,
									// userInfo: res.rawData,
									// encryptedData: encodeURIComponent(res.encryptedData),
									encryptedData: res.encryptedData,
									iv: res.iv,
									// signature: res.signature,
									// refereeId: uni.getStorageSync('refereeId')
								}, result => {
									console.log(result, 'result')
									// 记录token userId
									uni.setStorageSync('token', result.data.token);
									uni.setStorageSync('UserState', result.data.status);
									// uni.setStorageSync('userId', result.data.loginUserVo.userId);
									// self.cacheHelper.setUserInfo(res);
									// 执行回调函数,没有上一页,不走回调直接跳转
									// uni.navigateBack();
									if (result.data.registerFlag) {
										uni.switchTab({
											url: '/pages/index/index',
										})
									} else {
										// this.show = false;
										uni.navigateTo({
											url: '/pages/login/regitst'
										})
									}
									//  uni.switchTab({
									// url: '/pages/user/index/index'
									//  });
								}, false, () => {
									uni.hideLoading();
								});
							}
						});
					}
				});
			},
		},
	}
</script>

<style>
	.login-container {
		padding: 30rpx;
	}

	.wechatapp {
		padding: 80rpx 0 48rpx;
		border-bottom: 1rpx solid #e3e3e3;
		margin-bottom: 72rpx;
		text-align: center;
	}

	.wechatapp .header {
		width: 190rpx;
		height: 190rpx;
		border: 2px solid #fff;
		margin: 0rpx auto 0;
		border-radius: 50%;
		overflow: hidden;
		box-shadow: 1px 0px 5px rgba(50, 50, 50, 0.3);
	}

	.auth-title {
		color: #585858;
		font-size: 34rpx;
		margin-bottom: 40rpx;
	}

	.auth-subtitle {
		color: #888;
		margin-bottom: 88rpx;
		font-size: 28rpx;
	}

	.login-btn {
		padding: 0 20rpx;
	}

	.login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #04be01;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}

	.no-login-btn {
		margin-top: 20rpx;
		padding: 0 20rpx;
	}

	.no-login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: #dfdfdf;
		color: #fff;
		font-size: 30rpx;
		border-radius: 999rpx;
		text-align: center;
	}
</style>